package com.marvels.loanapply.api.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.loanapply.api.LoanExtDataApi;
import com.marvels.loanapply.channel.dao.UserMarvelChannelInfoMapper;
import com.marvels.loanapply.channel.dto.UserChannelInfoParamDTO;
import com.marvels.loanapply.channel.dto.UserChannelInfoResultDTO;
import com.marvels.loanapply.consum.LoanapplyConsumerFinancialApi;
import com.marvels.loanapply.dao.AcLoanApplyInfoDao;
import com.marvels.loanapply.dto.ApprovalProcessConditionDTO;
import com.marvels.loanapply.dto.ApprovalProcessInfoDTO;
import com.marvels.loanapply.dto.CollectAccountDTO;
import com.marvels.loanapply.dto.LoanExtDataDTO;
import com.marvels.loanapply.loan.dao.IStateDetailedDao;
import com.marvels.loanapply.loan.dto.StateDetailedDTO;

import cn.hutool.core.util.StrUtil;

@Service
public class LoanExtDataImpl implements LoanExtDataApi {

	@Autowired
	private IStateDetailedDao stateDetailedDao;
	
	@Autowired
	private AcLoanApplyInfoDao acLoanApplyInfoDao;
	
	@Autowired
	private LoanapplyConsumerFinancialApi consumerFinancialApi;
	@Autowired
	private UserMarvelChannelInfoMapper UserMarvelChannelInfoMapper;
	
	@Override
	public String selectLoanStateNameById(String stateId) {
		String stateName = stateId;
		MarvelsLogUtil.getInstance().info("根据进件状态id获取中文描述,stateId：" + stateId);
		StateDetailedDTO dto = new StateDetailedDTO();
		dto.setStateId(stateId);
		List<StateDetailedDTO> list =  stateDetailedDao.selectStateDetailed(dto);
		if(list != null && list.size() > 0){
			stateName = list.get(0).getStateExplain();
		}
		MarvelsLogUtil.getInstance().info("根据进件状态id获取中文描述,stateId：" + stateId + "，stateName:" + stateName);
		return stateName;
	}

	@Override
	public LoanExtDataDTO selectLoanExtData(LoanExtDataDTO dto){
		LoanExtDataDTO loanExtDataDTO = null;
		try {
			MarvelsLogUtil.getInstance().info("根据进件扩展信息,参数：" + JSONObject.toJSONString(dto));
			loanExtDataDTO = acLoanApplyInfoDao.selectLoanExtData(dto);
			if(loanExtDataDTO != null && StrUtil.containsAny(loanExtDataDTO.getStateId(), "D01", "A01", "A00")){
				CollectAccountDTO collectAccountDTO = consumerFinancialApi.selectAccountInfo(dto.getLoanId());
				if(collectAccountDTO != null){
					loanExtDataDTO.setLoanDate(collectAccountDTO.getLoanDate());
					loanExtDataDTO.setStatus(collectAccountDTO.getStatus());
				}
			}
			MarvelsLogUtil.getInstance().info("根据进件扩展信息,结果：" + JSONObject.toJSONString(loanExtDataDTO));
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("根据进件扩展信息异常", e);
		}
		return loanExtDataDTO;
	}

	@Override
	public List<UserChannelInfoResultDTO> selectUserChannelList(UserChannelInfoParamDTO dto) {
		return UserMarvelChannelInfoMapper.queryUserChannelByParam(dto);
	}

	@Override
	public PageInfo<ApprovalProcessInfoDTO> loanListQuery(ApprovalProcessConditionDTO dto) {
		PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
		List<ApprovalProcessInfoDTO> list = acLoanApplyInfoDao.loanListQuery(dto);
		PageInfo<ApprovalProcessInfoDTO> pageInfo = new PageInfo<ApprovalProcessInfoDTO>(list);
		return pageInfo;
	}

}
